package com.ddf.publics.service.impl;


import com.ddf.publics.dao.ZeroDao;
import com.ddf.publics.entity.Due;
import com.ddf.publics.entity.LoanApplication;
import com.ddf.publics.entity.LoanVo;
import com.ddf.publics.entity.User;
import com.ddf.publics.service.DueService;
import com.ddf.publics.service.LoanApplicationService;
import com.ddf.publics.service.UserService;
import com.ddf.publics.service.ZeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ZeroServiceimpl implements ZeroService {
    @Autowired
    ZeroDao zeroDao;

    @Autowired
    private DueService dueService;

    @Autowired
    private UserService userService;

    @Autowired
    private LoanApplicationService loanApplicationService;

    @Override
    public List<Map> AllLoan(int offset, int limit) {
        return zeroDao.AllLoan(offset,limit);
    }

    @Override
    public Map LoanbyId(int lid) {
        return zeroDao.LoanbyId(lid);
    }

    @Override
    public int QueryCount() {
        return zeroDao.QueryCount();
    }

    @Override
    public List<Map> LoanCondition(Map map) {
        return zeroDao.LoanCondition(map);
    }

    @Override
    public List<LoanApplication> queryAllByLimitUserId(int user_id, int offset, int limit) {
        return zeroDao.queryAllByLimitUserId(user_id,offset,limit);
    }

    @Override
    public LoanVo UserLoan(int lId) {
        return zeroDao.UserLoan(lId);
    }

    @Override
    public int LoanCount(int user_id) {
        return zeroDao.LoanCount(user_id);
    }

    @Override
    public void LoanDue(String keyid) {
        //借贷id
        String lid = keyid.substring(0, keyid.indexOf(","));
        //借款期数
        String qishu = keyid.substring(lid.length() + 1);
        //存入逾期表
        Due due = new Due();
        due.setLId(Integer.valueOf(lid));
        due.setDQishu(Integer.valueOf(qishu));
        due.setDTime(new Date());
        due.setDState(0);
        dueService.insert(due);
        //扣信用分
        LoanApplication loan = loanApplicationService.queryById(Integer.valueOf(lid));
        //扣除的分数
        int deductCredit = (int) (loan.getMonthMoney()/50);
        User user = userService.queryById(loan.getUserId());
        user.setCredit(user.getCredit()-deductCredit);
        userService.update(user);
    }

    @Override
    public int LatePayment(int state, int lId, int qishu) {
        return zeroDao.LatePayment(state,lId,qishu);
    }

    @Override
    public List<Map> loanEchars() {
        return zeroDao.loanEchars();
    }


}
